package org.tea.inventory.admin.web.admin;

import org.tea.inventory.admin.annotation.LoginAdmin;
import org.tea.inventory.base.util.ResponseUtil;
import org.tea.inventory.db.domain.NewsComment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.tea.inventory.db.service.NewsCommentService;

import java.util.*;


/**
 * @ClassName NewCommentController
 * @Description 评论管理Controller
 * @Author yangjh
 * @Date 2018/9/28 17:57
 * @Version 1.0
 */
@RestController
@RequestMapping("/admin/newsComment")
public class NewCommentController {

    @Autowired
    private NewsCommentService newsCommentService;

    /**
     * 获取评论列表
     * @param adminId
     * @param newsComment
     * @param page
     * @param limit
     * @param sort
     * @return
     */
    @GetMapping("listCommon")
    public Object list (@LoginAdmin Integer adminId,
                                            NewsComment newsComment,
                                            @RequestParam(value = "page", defaultValue = "1")Integer page,
                                            @RequestParam(value = "limit", defaultValue = "10")Integer limit,
                                            String sort){
        if(adminId == null){
            return ResponseUtil.fail401();
        }
        List<NewsComment> lstCommont = newsCommentService.selectCommonList(newsComment, page, limit, sort);
        int total = newsCommentService.countByComList(newsComment);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("items", lstCommont);
        data.put("total", total);
        return ResponseUtil.ok(data);
    }

    /**
     * 删除评论
     * @param adminId
     * @param newsComment
     * @return
     */
    @PostMapping("deleteCommon")
    public Object deleteCommon(@LoginAdmin Integer adminId, @RequestBody NewsComment newsComment) {
         if(adminId == null) {
          return ResponseUtil.unlogin();
         }

        if(newsComment.getId() == 0) {
            return ResponseUtil.badArgument();
        }

        newsCommentService.delete(newsComment.getId());

        return ResponseUtil.ok();
    }

    /**
     * 批量删除评论
     * @return
     */
    @PostMapping("deleteQuantity")
    public Object deleteQuantity(@LoginAdmin Integer adminId, String id) {
        if (adminId == null) {
            return ResponseUtil.unlogin();
        }
        if(id == null) {
            return ResponseUtil.badArgument();
        }
        newsCommentService.deleteQuantity(Arrays.asList(id.split(",")));
        return ResponseUtil.ok();
    }
}
